Latest update: July 2013
In this tutorial, we will show you how to view and change your FlashAir SSID and
network password
using
config.cgi. We will use the
set SSID operation to set a new SSID
and the
set network security key operation
to set the new password.
Additionally, since we are displaying the current SSID and password, we will
need to use
command.cgi. We will use
Get SSID and
Get network password to fetch the
current SSID and password respectively.
This is what the screen layout of the application should look like:
When the Get button is tapped, the app displays the current SSID in the labelSSID block and
the current
password in the labelPassword block.
When the Set button is tapped, the app will segue to the View Controller of the
registration
screen where the new SSID and password can be entered. Once the user is finished filling
out
the text fields, the Done button can be tapped to submit the set operations to the
FlashAir.
Warning: In order to set a new SSID or network password on your FlashAir device, you will need the MASTERCODE for your FlashAir device. This can be found by inserting your FlashAir into a PC or Mac and opening the CONFIG file of the FlashAir device. The CONFIG file is found in the "/SD_WLAN/" directory of the FlashAir device and should be opened in a text editor. You can check the Mastercode only when you finished FlashAir initial setting. There are hidden attributes attached to this directory, therefore we will use tools to handle this hidden folder.
We will add the following layout elements:
UIButton
)
UILabel
)
UIButton
)
UITextField
)
UIViewController
) UIScrollView
)
UINavigationController
)
The display screen looks like this:
This is the screen that appears when the Set button is tapped. We will implement this class later in the tutorial.
To get the current SSID and password, we use
command.cgi
with
op=104
and
op=105
. We will use
NSString stringWithContentsOfURL
to execute the CGI command. This
NSString
function will allow us to get the information at the specified URL in
UTF-8 encoding
and will return any error data in an
NSError
object.
@interface FSViewController : UIViewController
- (IBAction)buttonGetPush:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *labelSSID;
@property (strong, nonatomic) IBOutlet UILabel *labelPassword;
@end
- (IBAction)buttonGetPush:(id)sender {
NSError *error = nil;
// Get SSID
// Make url
NSURL *url104 = [NSURL URLWithString:@"http://flashair/command.cgi?op=104"];
// Run cgi
NSString *SSIDStr = [NSString stringWithContentsOfURL:url104
encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:self.title
message:@"op=104 Failed \n check access point" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
NSLog(@"error104 %@\n",error);
return;
}
// Get Password
// Make url
NSURL *url105 = [NSURL URLWithString:@"http://flashair/command.cgi?op=105"];
// Run cgi
NSString *passwordStr = [NSString stringWithContentsOfURL:url105
encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
message:@"op=105 Failed \n check access point" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
NSLog(@"error105 %@\n",error);
return;
}
self.labelSSID.text = SSIDStr;
self.labelPassword.text = passwordStr;
}
The above function implements the action done by the app after the user presses the Get button - which is fetching the SSID and network password for the FlashAir.
op=104
for the CGI command. encoding:NSUTF8StringEncoding
. op=104
for the CGI command. encoding:NSUTF8StringEncoding
. labelSSID
and
labelPassword
to display the string values of the fetched SSID and network
password.To get the SSID and network password, we will use
config.cgi
with
MASTERCODE
,
APPSSID
, and
APPNETWORKKEY
as parameters. We will use
NSString stringWithContentsOfURL
again to execute the CGI command.
@interface FSSetViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *textMASTERCODE;
@property (strong, nonatomic) IBOutlet UITextField *textSSID;
@property (strong, nonatomic) IBOutlet UITextField *textPassword1;
@property (strong, nonatomic) IBOutlet UITextField *textPassword2;
- (IBAction)buttonDonePush:(id)sender;
@end
- (IBAction)buttonDonePush:(id)sender {
NSError *error = nil;
// Check
NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
// MASTERCODE
NSString *mastercodeText = [self.textMASTERCODE.text
stringByTrimmingCharactersInSet:charSet];
if ([mastercodeText isEqualToString:@""]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
message:@"Enter MASTERCODE" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
}
// SSID
NSString *ssidText = [self.textSSID.text stringByTrimmingCharactersInSet:charSet];
if ([ssidText isEqualToString:@""]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
message:@"Enter SSID" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
}
// password
NSString *password1Text = [self.textPassword1.text
stringByTrimmingCharactersInSet:charSet];
NSString *password2Text = [self.textPassword2.text
stringByTrimmingCharactersInSet:charSet];
if ([password1Text isEqualToString:@""] || [password2Text isEqualToString:@""]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
message:@"Enter password" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
}else if(![password1Text isEqualToString:password2Text]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
message:@"Password mismatch!" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
}
// Set SSID and password
// Make url
NSString *urlStr = [@"http://flashair/config.cgi?MASTERCODE="
stringByAppendingString:mastercodeText];
urlStr = [urlStr stringByAppendingString:@"&APPNETWORKKEY="];
urlStr = [urlStr stringByAppendingString:password1Text];
urlStr = [urlStr stringByAppendingString:@"&APPSSID="];
urlStr = [urlStr stringByAppendingString:ssidText];
NSURL *url = [NSURL URLWithString:urlStr];
// Run cgi
NSString *rtnStr =[NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:&error];
if ([error.domain isEqualToString:NSCocoaErrorDomain]){
NSLog(@"config.cgi %@\n",error);
return;
}else{
if([rtnStr isEqualToString:@"ERROR"]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
message:@"config.cgi failed" delegate:nil
cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
return;
}
}
// Close this View
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
[textField resignFirstResponder];
return YES;
}
We have now set the behavior of the new SSID and password entry screen and the Done button. Once the text field input has been checked, we will submit the new SSID and password.
config.cgi?MASTERCODE=<yourMastercode>&APPNETWORKKEY=<yourNewSSID>&APPSSID=<yourNewPassword>
encoding:NSUTF8StringEncoding
.Once we finish writing the program, we will check to see if it works.
First, we will try tapping the Get button.
The SSID and password of the FlashAir have been displayed:
Next, we will tap the Set button. In order to set a new SSID and password, we will need the MASTERCODE (discussed earlier in the tutorial). Check the CONFIG file of the FlashAir device for this value:
Now I will enter the MASTERCODE, new SSID, and new password in their appropriate text fields.
Warning: Please be sure to remember the new SSID
and password that you entered.
Remember, you will need to set a network security password. None of the fields can
be left blank.
After you tap done, the SSID and password of the FlashAir have been changed.
Therefore, your
current Wireless LAN connection to the FlashAir is invalid and you will be disconnected
from
the FlashAir.
Now I will press the Done button on the bottom of the screen.
Since you have changed the SSID and password, you will need to reconnect to the new
SSID before
you can use the FlashAir's Wireless LAN functionality again.
Since our command was successful, we have been returned to the Get screen. After
connecting
to the new SSID in our Wireless LAN settings, we will press the Get button.
The screen will now display our new SSID and network password:
You have now completed the tutorial on getting and setting your SSID and network password.
ios_tutorial_06.zip (25KB)
All sample code on this page is licensed under BSD 2-Clause License